package com.dream.inspect.modules.business.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.SuperviseImageEntity;
import com.dream.inspect.modules.business.entity.SuperviseQuestionEntity;
import com.dream.inspect.modules.business.service.SuperviseImageService;
import com.dream.inspect.modules.business.service.SuperviseQuestionService;
import com.dream.inspect.modules.dream.pojo.SysUserFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;



/**
 * 督察问题表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:52
 */
@RestController
@RequestMapping("business/supervisequestion")
public class SuperviseQuestionController extends AbstractController {
    @Autowired
    private SuperviseQuestionService superviseQuestionService;
    @Autowired
    SuperviseImageService superviseImageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:supervisequestion:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = superviseQuestionService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/listMultiple")
    @RequiresPermissions("business:listMultiple")
    public R listMultiple(@RequestParam Map<String, String> params){
    	SysUserFormMap user = getSessionUser();
    	params.put("deptId", String.valueOf(user.getLong("deptId")));
        params.put("v2", user.get("roleId").toString());
        PageUtils page = superviseQuestionService.selectSuperviseQuestionListPage(params);

        return R.ok().put("page", page).put("user", user);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:supervisequestion:info")
    public R info(@PathVariable("id") Long id){
        SuperviseQuestionEntity superviseQuestion = superviseQuestionService.selectById(id);

        return R.ok().put("superviseQuestion", superviseQuestion);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:supervisequestion:save")
    public R save(@RequestBody SuperviseQuestionEntity superviseQuestion){
        superviseQuestionService.insert(superviseQuestion);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:supervisequestion:update")
    public R update(@RequestBody SuperviseQuestionEntity superviseQuestion){
        ValidatorUtils.validateEntity(superviseQuestion);
        superviseQuestionService.updateAllColumnById(superviseQuestion);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:supervisequestion:delete")
    public R delete(@RequestBody Long[] ids){
        superviseQuestionService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    /**
     * 督察记录问题详情信息
     */
    @RequestMapping("/questionDetailInfo")
    //@RequiresPermissions("business:superviserecord:info")
    public R questionDetailInfo(@RequestParam Map<String, String> params){

    	List<SuperviseQuestionEntity> resultList = superviseQuestionService.selectQuestionDetailInfo(params);

    	for(SuperviseQuestionEntity sqE : resultList) {
    		String sqId = String.valueOf(sqE.getId());
			String sqmId = String.valueOf(sqE.getModifyId());
			if (StringUtils.isNotEmpty(sqmId)) {
				List<SuperviseImageEntity> imageList = new ArrayList<SuperviseImageEntity>();
				
				Map<String, String> imageMap = new HashMap<String, String>();
	            imageMap.put("questionId", sqId);
	            imageMap.put("picType", "1");
	            imageMap.put("qf", "0");
	            imageList = superviseImageService.findSuperviseImageListByWeb(imageMap);
	            if (imageList != null && imageList.size() > 0) {
	            	sqE.setImageList(imageList);
	            }
	            
	            List<SuperviseImageEntity> fkImageList = new ArrayList<SuperviseImageEntity>();
				imageMap = new HashMap<String, String>();
	            imageMap.put("questionId", sqmId);
	            imageMap.put("picType", "2");
	            imageMap.put("qf", "0");
	            fkImageList = superviseImageService.findSuperviseImageListByWeb(imageMap);
	            if (fkImageList != null && fkImageList.size() > 0) {
	            	sqE.setFkImageList(fkImageList);
	            }
			}
			
		}
        return R.ok().put("questionList", resultList);
    }

}
